// pages/dati/dati.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xianshi:1,
    countDownHour: '00',
    countDownMinute: '00',
    countDownSecond: '00',
    maxtime: "",
    totalTestTime: "",
    isHiddenLoading: true,
    isHiddenToast: true,
    dataList: {},
    currentIndex:0,//当前题目index
    list:[],
    currentProblems: {//当前题目
      // title: "题目1", grade: 20, type: "1", buildingExamAnswerEOList:[

      //   { id: 1, createTime: null, updateTime: null, remark: null, title: 1111,status:'0' },
      //   { id: 2, createTime: null, updateTime: null, remark: null, title: 22222,status:'1'}
      // ]
    },
  },
  xianshi:function(){
    var xianshiif = this.data.xianshi;
    if (xianshiif==1){
      this.setData({
        xianshi:2
      })
    } else{
      this.setData({
        xianshi: 1
      })
    }
  },
  choose:function(e){
    var currentProblems = this.data.currentProblems;
    var list = this.data.list;
    var id = e.currentTarget.dataset.id;
    console.log(currentProblems);
    if (currentProblems.type == 1 || currentProblems.type == 3){//单选
      console.log('单选题')
      console.log(currentProblems.buildingExamAnswerEOList)
      console.log(currentProblems.buildingExamAnswerEOList[id].status);
      if (currentProblems.buildingExamAnswerEOList[id].status == '0'){
        currentProblems.buildingExamAnswerEOList.map(function (item) {
          item.status = '0'
        });
        console.log('设为选中')
        currentProblems.buildingExamAnswerEOList[id].status='1'//设为选中
      } else if (currentProblems.buildingExamAnswerEOList[id].status === '1'){
        console.log('取消选中')
        currentProblems.buildingExamAnswerEOList[id].status = '0'//取消选中

      }
    } else if (currentProblems.type == 2){//多选
      console.log('多选题');
      if (currentProblems.buildingExamAnswerEOList[id].status == '0') {
        console.log('设为选中')
        currentProblems.buildingExamAnswerEOList[id].status = '1'//设为选中
      } else if (currentProblems.buildingExamAnswerEOList[id].status === '1') {
        console.log('取消选中')
        currentProblems.buildingExamAnswerEOList[id].status = '0'//取消选中
      }
    }
    
    currentProblems.buildingExamAnswerEOList.some(function (value, index){//判断该题是否为已做
          console.log(value)
            return value.status == '1'
    }) ? currentProblems.status = '1' : currentProblems.status = '0';
   
    list[this.data.currentIndex] = currentProblems;
    this.setData({
      currentProblems: currentProblems,
      list:list
    });
    console.log(this.data.currentProblems)
  },
  
  // 切换题目
  changeProblems(i){
    this.setData({
      currentProblems:this.data.list[i],
    })
  },
  nextP(){//下一题
    var _index = this.data.currentIndex
    if (_index >=this.data.list.length-1){
      return false
    }
    _index++;
    this.setData({
      currentIndex : _index
    })
    this.changeProblems(_index)
  },
  prevP() {//上一题
    var _index = this.data.currentIndex
    if (_index == 0) {
      return false
    }
    _index--;
    this.setData({
      currentIndex: _index
    })
    this.changeProblems(_index)
  },
  //提交试卷
  goSumbit(e){
      //1.验证题目是否答完
    var list = this.data.list;
    var pstr = '';
    for (var i = 0; i < list.length; i++) {
      if (list[i].status === '0') {
        pstr += i+1 + '、';
      }
    }
    if (list.some(function(item){ return item.status==='0'})){
      wx.showModal({
        title: '请先完成答题',
        content: pstr + '题',
        showCancel:false
      })
    }else{//发送提交
      var userId = app.getLoginInfo().id;
      var testTime = e.currentTarget.dataset.totaltesttime;
      wx.request({
        url: app.globalData.url+'/exam/examSubmit',
        data: {
          'testTime': testTime,
          'buildingExamTopicVOS': list,
          'userId': userId,
        },
        method: 'POST',
        success(res){
          if (res.data.status == "success") {
            wx.navigateTo({
              url: '../datijieguo/datijieguo?res=' + JSON.stringify(res.data.result)
            })
          }
        }
      })
    }
  },
  setTimeOut(mins) {//倒计时函数
    var totalSecond = mins * 60;
    console.log(totalSecond)
    var interval = setInterval(function () {
      // 秒数
      var second = totalSecond;

      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      this.setData({
        // countDownDay: dayStr,
        // countDownHour: hrStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;

      if (totalSecond < 0) {
        clearInterval(interval);
        wx.showToast({
          title: '考试结束',
        });
        this.setData({
          // countDownDay: '00',
           countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
        });
      }
    }.bind(this), 1000);
  },


  onLoad: function (e) {
    var that = this;
    var id = e.id;
    // 获取题目
    wx.request({
      url: app.globalData.url + '/exam/' + id,
      data: {},
      success(res) {
        console.log(res);
        if (res.data.status ==='success'){
          that.setData({
            list:res.data.result.list,
            totalTestTime: res.data.result.dateNew
          })
          console.log(that.data.list)
          that.changeProblems(0)//初始化当前题目为第一题
          that.setTimeOut(res.data.result.timeInterval);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})